package com.java.controller;

import cn.hutool.core.date.DateUtil;
import com.java.config.result.Result;
import com.java.model.cmn.Dict;
import com.java.service.DictService;
import io.swagger.annotations.Api;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

@Api(tags = "数据字典")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     *根据id查询子集
     * @param id
     * @return
     */
    @GetMapping("/getDicts/{id}")
    public Result getList(@PathVariable("id") Long id){
        List<Dict> children = dictService.findChildById(id);
        return Result.ok(children);
    }

    /**
     * Excel导出
     * @param response
     * @return
     */
    @GetMapping("/exportData")
    public Result exportExcel(HttpServletResponse response){
        dictService.exporExcel(response);

        return Result.ok();
    }

    /**
     * excel导入
     * @param file
     * @return
     */
    @GetMapping("/importData")
    public Result importExcel(MultipartFile file){
        dictService.importExcel(file);
        return Result.ok();
    }

    /**
     * 根据dictCode = "Hostype"父级code,子集元素的value查询该元素name
     * @param dictCode
     * @param value
     * @return
     */
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value){
        String name = dictService.findName(dictCode,value);
        return name;
    }

    /**
     * 查询子集该元素name
     * @param value
     * @return
     */
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value){
        String name = dictService.findName("",value);
        return name;
    }

    /**
     *获取省
     * @param typeCode
     * @return
     */
    @GetMapping("findBydictCode/{typeCode}")
    public Result getTypeCode(@PathVariable String typeCode){
        List<Dict> list = dictService.findNameAndChild(typeCode);
        return Result.ok(list);
    }

    @GetMapping("finddictCode/{typeCode}")
    public Result getProvinceCode(@PathVariable String typeCode){
        List<Dict> list = dictService.findNameChildren(typeCode);
        return Result.ok(list);
    }
}
